home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / mdl10.zoo / mkdl.c < prev    next >
C/C++ Source or Header  |  1993-06-06  |  6KB  |  317 lines

  1. /*
  2.  *    mkdl -- create a sample dl file
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>        /* for malloc */
  7. #include <string.h>        /* for bzero */
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10.  
  11.  
  12.  
  13. typedef struct {
  14.     int    version;
  15.     int    format;
  16.     int    images_per_screen;
  17.     char    title[21];
  18.     char    author[21];
  19.     int    num_screen;
  20.     int    num_command;
  21. } DL_info;
  22.  
  23.  
  24.  
  25. int        __default_mode__ = _IOBIN;    /* force stdin binary (gcc) */
  26. char           *myname = "mkdl";
  27.  
  28.  
  29. void        colormap_setup (FILE *, int);
  30.  
  31.  
  32.  
  33. /*------------------------------*/
  34. /*    main            */
  35. /*------------------------------*/
  36. void main (int argc, char *argv[])
  37. {
  38.     DL_info        dlinfo;        /* dl file info */
  39.     char           *filename;
  40.     FILE           *fp;
  41.     unsigned char  *image_data;    /* space for 1 screen from file */
  42.     int           *cmd;        /* space for command list */
  43.     int        i, j;
  44.  
  45.  
  46.  
  47.  
  48.     /*
  49.      *   if no files, use stdin. otherwise open file
  50.      */
  51.     argv++, argc--;
  52.     if (argc < 1)
  53.     {
  54.         fprintf(stderr, "%s: need file name\n", myname);
  55.         exit(1);
  56.     }
  57.     else if ((fp = fopen (*argv, "wb")) == NULL)
  58.     {
  59.         fprintf(stderr, "%s: can't open %s\n", myname, argv[1]);
  60.         exit(1);
  61.     }
  62.     filename = *argv;
  63.  
  64.  
  65.  
  66.     /*
  67.      *   set up parameters...
  68.      */
  69.     dlinfo.version           = 2;
  70.     dlinfo.format            = 1;
  71.     dlinfo.images_per_screen = 4;
  72.     dlinfo.num_screen        = 1;
  73.     dlinfo.num_command       = 16;
  74.     for (i = 0; i < 20; i++)
  75.     {
  76.         dlinfo.title[i]  = 0;
  77.         dlinfo.author[i] = 0;
  78.     }
  79.  
  80.  
  81.  
  82.     /*
  83.      *   write the version number...
  84.      */
  85.     fputc (dlinfo.version, fp);
  86.  
  87.     
  88.  
  89.     /*
  90.      *   ...and the format.
  91.      */
  92.     fputc (dlinfo.format, fp);
  93.  
  94.  
  95.  
  96.     /*
  97.      *   put title and author
  98.      */
  99.     for (i = 0; i < 20; i++)
  100.         fputc ((int)(dlinfo.title[i] ^ 255), fp);
  101.     for (i = 0; i < 20; i++)
  102.         fputc ((int)(dlinfo.author[i] ^ 255), fp);
  103.     
  104.  
  105.  
  106.     /*
  107.      *   number of screens and commands.
  108.      */
  109.     fputc(dlinfo.num_screen, fp);
  110.     fputc(dlinfo.num_command, fp);
  111.     
  112.  
  113.  
  114.     /*
  115.      *   Display what we know so far.
  116.      */
  117.     printf("%s is a %s sized version %d .DL file.\n",
  118.         filename,
  119.         (dlinfo.format == 0 ? "large" : "medium"), 
  120.         dlinfo.version);
  121.     printf("It contains %d images (num_screen=%d * images_per_screen=%d)\n",
  122.         dlinfo.num_screen * dlinfo.images_per_screen,
  123.         dlinfo.num_screen,
  124.         dlinfo.images_per_screen);
  125.     printf("in a %d frame (num_command) loop.\n",
  126.         dlinfo.num_command);
  127.  
  128.  
  129.  
  130.     /*
  131.      *   do the color map
  132.      */
  133.     colormap_setup(fp, dlinfo.version);
  134.  
  135.  
  136.  
  137.     /*
  138.      *   Allocate memory for the commands, the image data, etc
  139.      */
  140.     printf ("Allocating memory:\n");
  141.  
  142.     /* command list: */
  143.     printf ("   command list (%d bytes)\n",
  144.         dlinfo.num_command * sizeof(int));
  145.     if (!(cmd = (int *)malloc(dlinfo.num_command * sizeof(int))))
  146.     {
  147.         fprintf(stderr, "%s: out of memory (commands)", myname);
  148.         exit(1);
  149.     }
  150.  
  151.     /* one screen (file): */
  152.     printf ("   single screen from file (64000 bytes)\n");
  153.     if (NULL == (image_data = (unsigned char *)malloc(320 * 200)))
  154.     {
  155.         fprintf(stderr, "%s: not enough memory (image data).", myname);
  156.         exit(1);
  157.     }
  158.  
  159.  
  160.  
  161.     /*
  162.      *   Build the pixmaps for the animation.
  163.      */
  164.     printf("Building pixmaps, wait..."); fflush(stdout);
  165.     for (j = 0; j < dlinfo.num_screen; j++)
  166.     {
  167.         /*
  168.          *   image is stored in 'screens' which can have 1 or 4
  169.          *   images. with 'medium' format in version 2, layout is:
  170.          *
  171.          *      0 <-------row------>319
  172.          *     _____________________  0
  173.          *    |          |          | ^
  174.          *    | screen 0 | screen 1 | |
  175.          *    |          |          | |
  176.          *    |__________|__________| col
  177.          *    |          |          | |
  178.          *    | screen 2 | screen 3 | |
  179.          *    |          |          | v
  180.          *    |__________|__________| 199
  181.          *
  182.          *   otherwise there is a single image per screen. we try
  183.          *   and save space for this test.
  184.          *
  185.          *   the image here is just a white vertical bar on a black
  186.          *   background that moves right in each successive screen.
  187.          *   the commands will animate it.
  188.          */
  189.         unsigned char  *dst;
  190.         int        row;
  191.         int        col;
  192.  
  193.         dst = (unsigned char *) image_data;
  194.  
  195.         /* set everything black first */
  196.         bzero (dst, (size_t)(320*200));
  197.  
  198. /* image 0 */    for (row = 30; row < 60; row++)
  199.         {
  200.           for (col = 20; col < 30; col++)   dst[row*320 + col] = 255;
  201.         }
  202. /* image 1 */    for (row = 30; row < 60; row++)
  203.         {
  204.           for (col = 185; col < 195; col++) dst[row*320 + col] = 255;
  205.         }
  206. /* image 2 */    for (row = 130; row < 160; row++)
  207.         {
  208.           for (col = 30; col < 40; col++)   dst[row*320 + col] = 255;
  209.         }
  210. /* image 3 */    for (row = 130; row < 160; row++)
  211.         {
  212.           for (col = 195; col < 205; col++) dst[row*320 + col] = 255;
  213.         }
  214.  
  215.  
  216.         /*
  217.          *   one screen of data.
  218.          */
  219.         fwrite(image_data, 1, 320 * 200, fp);
  220.  
  221.     }
  222.     printf("done\n");
  223.  
  224.  
  225.  
  226.     /*
  227.      *   write the commands.
  228.      */
  229.     cmd[0]  = 0;
  230.     cmd[1]  = 1;
  231.     cmd[2]  = 2;
  232.     cmd[3]  = 3;
  233.     cmd[4]  = 2;
  234.     cmd[5]  = 1;
  235.     cmd[6]  = 2;
  236.     cmd[7]  = 3;
  237.     cmd[8]  = 2;
  238.     cmd[9]  = 3;
  239.     cmd[10] = 2;
  240.     cmd[11] = 3;
  241.     cmd[12] = 2;
  242.     cmd[13] = 1;
  243.     cmd[14] = 2;
  244.     cmd[15] = 1;
  245.  
  246.     for (i = 0; i < dlinfo.num_command; i++)
  247.     {
  248.         if (dlinfo.version == 2)
  249.         {
  250.             fputc(cmd[i], fp);
  251.             fputc(0, fp);        /* as long as number < 255 */
  252.         }
  253.         else
  254.         {
  255.             /*
  256.              *   unknown???   here is the way it is read:
  257.              *
  258.              *    j = fgetc(fp);
  259.              *    cmd[i] = (j % 10) - 1 + ((j / 10) - 1) * 4;
  260.              */
  261.             fputc(cmd[i], fp);
  262.         }
  263.     }
  264.  
  265.     fclose (fp);
  266.  
  267.     exit (0);
  268. }
  269.  
  270.  
  271.  
  272.  
  273.  
  274. /*------------------------------*/
  275. /*    colormap_setup        */
  276. /*------------------------------*/
  277. void colormap_setup (FILE *fp, int version)
  278. {
  279.     unsigned char    pal[768];
  280.     int        i;
  281.  
  282.  
  283.     /*
  284.      *   set up palette (continuous gray)...
  285.      */
  286.     for (i = 0; i < 256; i++)
  287.     {
  288.         pal[3*i    ] = i;
  289.         pal[3*i + 1] = i;
  290.         pal[3*i + 2] = i;
  291.     }
  292.  
  293.  
  294.     /*
  295.      *   Is this the border colour? mdl ignores this anyway...
  296.      */
  297.     if (version == 2)
  298.     {
  299.         for (i = 0; i < 3; i++)
  300.             fputc(0, fp);
  301.     }
  302.     else
  303.         fputc(0, fp);
  304.  
  305.  
  306.  
  307.     /*
  308.      *   write the colormap. 3 bytes per each of 256 colors.
  309.      */
  310.     fwrite(pal, 1, 768, fp);
  311.  
  312.     return;
  313. }
  314.  
  315.  
  316.  
  317.